import axios from "axios";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

export function request(config) {
  //1.创建axios实例
  let instance = axios.create({
    baseURL: "https://qct.yskjtz.com/api/",
    timeout: 10000,
  });

  // 2.axios的拦截器
  // 2.1.请求拦截的作用
  instance.interceptors.request.use(
    (config) => {
      let token = localStorage.getItem("token");
      console.log("token", token);
      config.headers["Authorization"] = token;
      NProgress.start();
      return config;
    },
    (err) => {
      NProgress.done();
      return err;
    }
  );

  // 2.2.响应拦截
  instance.interceptors.response.use(
    (res) => {
      NProgress.done();
      return res;
    },
    (err) => {
      NProgress.done();
      return err;
    }
  );

  // 3.发送真正的网络请求
  return instance(config);
}
